package com.education.teach.mvp.presenter;

import android.app.Application;

import com.education.teach.mvp.model.bean.CounselBean;
import com.education.teach.mvp.model.bean.ExamBean;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.education.teach.mvp.contract.CounselContract;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.HashMap;
import java.util.Map;


@ActivityScope
public class CounselPresenter extends BasePresenter<CounselContract.Model, CounselContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public CounselPresenter(CounselContract.Model model, CounselContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void consulQuestionList(String xsXlh, int page, int maxRow, boolean isRefreh) {
        Map<String, String> map = new HashMap<>();
        map.put("yhxlh", xsXlh);
        map.put("currentPage", page + "");
        map.put("maxRows", maxRow + "");
        mModel.consulQuestionList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(disposable -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .subscribe(new ErrorHandleSubscriber<CounselBean>(mErrorHandler) {
                    @Override
                    public void onNext(CounselBean counselBean) {
                        if (isRefreh)
                            mRootView.returnRefreshQuestionList(counselBean.getResult().getQueryList());
                        else
                            mRootView.returnLoadMoreQuestionList(counselBean.getResult().getQueryList());
                    }

                });
    }
}
